#!/bin/sh

DAEMON="dnsmasq"
PIDFILE="/var/run/$DAEMON.pid"

[ -f /etc/dnsmasq.conf ] || exit 0

start() {
	printf "Starting dnsmasq: "
	start-stop-daemon --start --pidfile "$PIDFILE" \
		--exec "/usr/sbin/$DAEMON" \
		-- --pid-file="$PIDFILE"
	status=$?
	[ "$status" -eq 0 ] && echo "OK" || echo "FAIL"
	return "$status"
}

stop() {
	printf "Stopping dnsmasq: "
	start-stop-daemon --stop --pidfile "$PIDFILE" \
		--exec "/usr/sbin/$DAEMON"
	status=$?
	if [ "$status" -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
		return "$status"
	fi
	while start-stop-daemon --stop --test --quiet --pidfile "$PIDFILE" \
		--exec "/usr/sbin/$DAEMON"; do
		sleep 0.1
	done
	rm -f "$PIDFILE"
	return "$status"
}

restart() {
	stop
	start
}

case "$1" in
	start|stop|restart)
		"$1"
		;;
	reload)
		# Restart, since there is no true "reload" feature.
		restart
		;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
esac

exit 0
